package com.yonyou.pmclouds.plan.rmiimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.billstatus.BillStatusConst;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoInsertProcessor;
import com.yonyou.pmclouds.basecom.bp.processor.AuditInfoUpdateProcessor;
import com.yonyou.pmclouds.basecom.bp.template.SingleInsertBpTemplate;
import com.yonyou.pmclouds.basecom.bp.template.SingleUpdateBpTemplate;
import com.yonyou.pmclouds.basecom.consts.BillTypeConst;
import com.yonyou.pmclouds.basecom.entity.SuperVO;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.plan.entity.PlanProgressHistoryVO;
import com.yonyou.pmclouds.plan.entity.PlanProgressVO;
import com.yonyou.pmclouds.plan.entity.PlanWriteVO;
import com.yonyou.pmclouds.plan.mapper.PlanProgressHistoryMapper;
import com.yonyou.pmclouds.plan.mapper.PlanProgressMapper;
import com.yonyou.pmclouds.plan.mapper.PlanWriteMapper;
import com.yonyou.pmclouds.plan.rmiitf.PlanWriteMaintain;
import com.yonyou.pmclouds.workflow.service.rmiitf.IFlowApproveBuziService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@Deprecated
@Service(version = ApplicationConsts.APPLICATION_VERSION, interfaceClass = PlanWriteMaintain.class)
public class PlanWriteMaintainImpl implements PlanWriteMaintain, IFlowApproveBuziService {

    @Autowired
    private PlanWriteMapper planWriteMapper;
    @Autowired
    private PlanProgressMapper planProgressMapper;
    @Autowired
    private PlanProgressHistoryMapper planProgressHistoryMapper;

    @Override
    public PlanWriteVO savePlanWrite(PlanWriteVO planWriteVO) throws BusinessException {
        SingleInsertBpTemplate<PlanWriteVO> bp = new SingleInsertBpTemplate<>(planWriteMapper);
        bp.addBeforeProcessor(new AuditInfoInsertProcessor<PlanWriteVO>());
        bp.insert(planWriteVO);
        return planWriteVO;
    }

    @Override
    public SuperVO approveByPkBill(String pk_bill, byte billStatus) throws BusinessException {
        PlanWriteVO planWriteVO = planWriteMapper.queryBypkPlanWrite(pk_bill);
        SingleUpdateBpTemplate<PlanWriteVO> bp = new SingleUpdateBpTemplate<>(planWriteMapper, new String[]{PlanWriteVO.BILLSTATUS});
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<PlanWriteVO>());
        planWriteVO.setBillstatus(billStatus);
        bp.update(planWriteVO);
        return planWriteVO;
    }

    @Override
    public SuperVO rejectByPkBill(String pk_bill) throws BusinessException {
        PlanWriteVO planWriteVO = planWriteMapper.queryBypkPlanWrite(pk_bill);

        //更新状态为驳回态
        SingleUpdateBpTemplate<PlanWriteVO> bpTemplate = new SingleUpdateBpTemplate<>(planWriteMapper, new String[]{PlanWriteVO.BILLSTATUS});
        bpTemplate.addBeforeProcessor(new AuditInfoUpdateProcessor<PlanWriteVO>());
        planWriteVO.setBillstatus(BillStatusConst.COMMON_REJECT_STATUS);
        bpTemplate.update(planWriteVO);

        //利用pk_planwrite查询出来本次修改的子计划ID
        PlanProgressHistoryVO[] planProgressHistoryVOS = planProgressHistoryMapper.queryProgressHistoryByPkPlanWrite(pk_bill);
        //本次修改计划ID主键集合
        Set<String> pkPlans=new HashSet<>();
        for (PlanProgressHistoryVO planProgressHistoryVO:planProgressHistoryVOS){
            pkPlans.add(planProgressHistoryVO.getPkPlan());
        }
        //查询出本次修改后计划进度的数据
        PlanProgressVO[] planProgressVOS = planProgressMapper.queryByPlanIds(pkPlans.toArray(new String[0]));
        //查询出修改的子计划对应的上一次不是驳回态的修改历史记录
        PlanProgressHistoryVO[] lastPlanProgress = planProgressHistoryMapper.queryEffectiveProgressHisByPlans(pkPlans.toArray(new String[0]));
        //以pkPlan为主键进行分组
        Map<String,PlanProgressHistoryVO> planProgressHistoryVOMap=new HashMap<>();
           if (ArrayUtils.isNotEmpty(lastPlanProgress)){
               for (PlanProgressHistoryVO planProgressHistoryVO:lastPlanProgress){
                   planProgressHistoryVOMap.put(planProgressHistoryVO.getPkPlan(),planProgressHistoryVO);
               }
           }
        //驳回数据记录
        SingleUpdateBpTemplate<PlanProgressVO> bp = new SingleUpdateBpTemplate<>(planProgressMapper, new String[]{PlanProgressVO.PROGRESS, PlanProgressVO.START_DATE, PlanProgressVO.END_DATE});
        bp.addBeforeProcessor(new AuditInfoUpdateProcessor<PlanProgressVO>());
        if (ArrayUtils.isNotEmpty(planProgressVOS)){
            for (PlanProgressVO planProgressVO:planProgressVOS){
                //判断该子计划是否有不是驳回态的历史记录
                PlanProgressHistoryVO planProgressHistoryVO=planProgressHistoryVOMap.get(planProgressVO.getPkPlan());
                if (planProgressHistoryVO!=null){
                    planProgressVO.setStartDate(planProgressHistoryVO.getStartDate());
                    planProgressVO.setEndDate(planProgressHistoryVO.getEndDate());
                    planProgressVO.setProgress(planProgressHistoryVO.getProgress());
                    bp.update(planProgressVO);
                }else {
                    planProgressVO.setStartDate(null);
                    planProgressVO.setEndDate(null);
                    planProgressVO.setProgress(0);
                    bp.update(planProgressVO);
                }
            }
        }
        return null;
    }

    @Override
    public SuperVO queryDetailForWorkFlow(String pk_bill) throws BusinessException {
        return planWriteMapper.queryBypkPlanWrite(pk_bill);
    }

    @Override
    public String getBillType() {
        return BillTypeConst.PROCESS_WRITE_BILLTYPE;
    }

    @Override
    public SuperVO deleteBill(SuperVO businessVO) throws BusinessException {
        return null;
    }

    @Override
    public Object getMapper() {
        return planWriteMapper;
    }
}

